﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataModel;

namespace SocialPhotos
{
    /// <summary>
    /// Summary description for Comment
    /// </summary>
    public class CommentPoster : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int photoId = Int32.Parse(context.Request.Form["photoId"]);
                int userId = Int32.Parse(context.Request.Form["userId"]);
                string comment = context.Request.Form["comment"];

                SocialPhotoModelContainer model = new SocialPhotoModelContainer();
                Photo photo = model.Photos.Single(p => p.Id == photoId);
                User user = model.Users.Single(p => p.Id == userId);

                Comment pComment = new Comment { User = user, Photo = photo, Content = comment, PostedTime = System.DateTime.Now };
                model.AddToComments(pComment);
                model.SaveChanges();

                context.Response.ContentType = "text/plain";
                context.Response.Write("Comment is posted");
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Comment is rejected");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}